package JUC.pool;

import java.util.concurrent.*;

/**
 * @author mubei
 * @date 2021/11/21
 * @description
 */
public class Demo02 {

    public static void main(String[] args) {

        /*
        * 最大线程数如何定义
        * 1.cpu密集型   几核就是几  可以保证 cpu的效率最高
        *  Runtime.getRuntime().availableProcessors(),
        * 2.io密集型
        *       程序: 15个大型任务  io十分占资源  >=15
        * */
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2,
                Runtime.getRuntime().availableProcessors(),
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );


        try {
            for (int i = 0; i < 10; i++) {
                poolExecutor.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "ok");
                });
            }
        } finally {
            poolExecutor.shutdown();
        }
    }
}